﻿using System;
using System.Collections.Generic;
using System.Linq;
using JustinAngel.MetroBlog.Services.Data;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Shared.Common.Logging;

namespace JustinAngel.MetroBlog.UI.Core.Email
{
    public class CommentEmailService : ICommentEmailService
    {
        #region Implementation of ICommentEmailService

        [Dependency]
        public IEmailService EmailService { get; set; }

        public void SendEmail(Comment comment, BlogPost blogpost)
        {
            try
            {
                SendEmailToBlogOwner(comment, blogpost);
                SendEmailToEveryoneOnThread(comment, blogpost);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }

        private void SendEmailToEveryoneOnThread(Comment comment, BlogPost blogpost)
        {

            string Subject = "New comment on " + blogpost.Title;
            string To = blogpost.User.Email;

            string blogPostLink = null;
            if (blogpost.User.IsDefaultBlog.HasValue && blogpost.User.IsDefaultBlog.Value)
                 blogPostLink = blogpost.User.HomeUrl + "/" + blogpost.Permlink;
            else
                blogPostLink = blogpost.User.HomeUrl + "/" + blogpost.User.Username + "/" + blogpost.Permlink;
               
            string Body = string.Format("New comment on {0} by {1}: <br /><br />{2}<br/><br/><a href=\"{3}\">{4}</a>",
                                        blogpost.Title, 
                                        comment.Name, 
                                        comment.HTML, 
                                        blogPostLink, 
                                        blogPostLink);


            var CommentersEmails = GetAllParents(comment).Select(c => c.Email).Distinct().ToList();
            foreach (var email in CommentersEmails)
            {
                try { EmailService.SendEmail(email, To, Subject, Body); }
                catch { }
            }
        }

        private IEnumerable<Comment> GetAllParents(Comment comment)
        {
            if (comment.ParentComment != null)
            {
                yield return comment.ParentComment;
                foreach (var parent in GetAllParents(comment.ParentComment))
                    yield return parent;
            }                
        }

        private void SendEmailToBlogOwner(Comment comment, BlogPost blogpost)
        {
            string ToAndFrom = blogpost.User.Email;
            string Subject = "New comment - " + blogpost.Title;
            string blogPostLink = null;
            if (blogpost.User.IsDefaultBlog.HasValue && blogpost.User.IsDefaultBlog.Value)
                blogPostLink = blogpost.User.HomeUrl + "/" + blogpost.Permlink;
            else
                blogPostLink = blogpost.User.HomeUrl + "/" + blogpost.User.Username + "/" + blogpost.Permlink;

            string body = string.Format("New comment on {0} by {3} ({4}, {5})<br /><br/>{1}<br/><br/><a href=\"{6}\">{0}<br/><br/></a>See management console at: <a href=\"{2}/Management\">here</a>",
                                        blogpost.Title,
                                        comment.HTML,
                                        blogpost.User.HomeUrl,
                                        comment.Name,
                                        comment.Url,
                                        comment.Email,
                                        blogPostLink);

            try { EmailService.SendEmail(ToAndFrom, ToAndFrom, Subject, body); }
            catch { }
        }

        #endregion
    }
}
